<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>

<html>
	<head>
		<title>计量单位管理</title>
	</head>
	<script type="text/javascript">
	
		var lastIndex=-1;
		$(function(){
			$("#unit").keypress(function(e) { 
     	  		if(e.which == 13) { 
   					addUnit();
       			} 
   			}); 
			$('#btn').linkbutton();
			$('#table').datagrid({
				title:'计量单位',
				iconCls:'icon-edit',
				rownumbers:true,
				url:'<%=contextPath %>/springMVC/group/listPageUnit',
				loadMsg:'正在加载数据，请稍后......',
				fitColumns:true,
				idField:'id',
				frozenColumns:[[
	                {field:'ck',checkbox:true},
				    {title:'编号',field:'id',hidden:true}
				]],
				columns:[[
					{title:'计量单位',field:'name',fit:true,width:100,align:'center',editor:{type:'text'}}
				]],
				pageSize:10,
				pageList : [10,50,100],  
				pagination:true,
				toolbar:[{
						id:'add',
						text:'新增',
						iconCls:"icon-add",
						handler:function(){
							$('#table').datagrid('endEdit', lastIndex);
							$("#addUnit").show();
							$("#unit").focus();
						}
					},'-',
					{
						id:'save',
						text:'保存',
						iconCls:"icon-save",
						handler:function(){
							updateUnit();
						}
					},'-',
					{
					id:'delete',
					text:'删除',
					disabled:false,
					iconCls:'icon-remove',
					handler:function(){
							var rows=$('#table').datagrid('getSelections');
							var deleteVar="";
							if(rows.length==0)
							{
								$.messager.alert('提示','选中要删除的记录');  
								return ;
							}
							for(var i=0;i<rows.length;i++)
							{
								if(i==(rows.length-1))
								{
									deleteVar+=rows[i].id;
								}else
								{
									deleteVar+=rows[i].id+",";
								}
							}
							$.messager.confirm('确认','您确认删除该记录信息？',function(msg){
								if(msg){
									$.ajax({
										type:'POST',
										url:"<%=contextPath %>/springMVC/group/deleteUnit",
										data:"ids="+deleteVar,
										dataType:"json",
										success:function(rtJson){
											if(rtJson.rtState == '0'){
												table_reload(rows.length);
											}
											else{
												warningInfo(rtJson.rtMsrg);
											}
										}
									});
								}
							});
					}
				}],
				onLoadSuccess:function(rtJson){
					if(rtJson.rtState== '1')
					{
						warningInfo(rtJson.rtMsrg);
					}
				},
				onDblClickRow:function(rowIndex){
					if (lastIndex != rowIndex){
						$('#table').datagrid('endEdit', lastIndex);
						$('#table').datagrid('beginEdit', rowIndex);
						$("input[class='datagrid-editable-input']").keypress(function(e) { 
			     	  		if(e.which == 13) { 
			     	  			updateUnit();
			       			} 
			   			}); 
					}
					lastIndex = rowIndex;
				}
			});
			//--end 
		});
		function updateUnit(){
			if(lastIndex == -1){return;}
			$('#table').datagrid('endEdit', lastIndex);
			var rows = $('#table').datagrid('getRows');
			$.ajax({
				type:'POST',
				url:'<%=contextPath %>/springMVC/group/updateUnit',
				data:'id='+rows[lastIndex].id+'&name='+rows[lastIndex].name,
				dataType:'json',
				success:function(rtJson){
					if(rtJson.rtState == '0'){
						warningInfo(rtJson.rtMsrg);
					}
					else{
						$('#table').datagrid('rejectChanges');
						warningInfo(rtJson.rtMsrg);
					}
					lastIndex = -1;
				}
			});
		}
		function addUnit(){
			if($("#unit").val() == ""){
				warningInfo("请填写计量单位");
				$("#unit").focus();
				return;
			}
			$.ajax({
				url:"<%=contextPath %>/springMVC/group/addUnit",
				type:'POST',
				data:"name="+$("#unit").val(),
				dataType:'json',
				success:function(rtJson){
					if(rtJson.rtState == '0'){
						$('#table').datagrid('reload');
						$("#addUnit").hide();
					}
					else{
						warningInfo(rtJson.rtMsrg);
					}
					$("#unit").val("");
				}
			});
		}
	</script>
	<body>
		<div id="addUnit" class="easyui-panel" style="display: none;">
				<table>
					<tr>
						<td><span>添加单位：</span></td>
						<td><input type="text" id="unit" name="unit" data-options="required:true"></td>
						<td><a href="#" id="btn" iconCls="icon-add" onclick="addUnit();">添加</a></td>
					</tr>
				</table>
		</div>
		<div id="table"></div>
		
	</body>
</html>
